package com.pj.app.aura;

import com.pj.lib.aura.connection.Result;
import com.pj.lib.aura.protocol.AuraProtocolMapKeys;
import com.pj.lib.auraClient.Aura;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateAccountActivity extends Activity {
	public static final String NICK_NAME = "1";
	public static final String PASSWORD = "2";
	public static final String USER_ID = "3";
	
	EditText editTextLogin;
	EditText editTextPassword;
	EditText editTextPasswordRepeat;
	Button buttonCreateAccount;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_account_activity);
		
		setWidgets();
		
	}
	private void setWidgets() {
		editTextLogin = (EditText) findViewById(R.id.create_account_username_field);
		editTextPassword = (EditText) findViewById(R.id.create_account_password_field);
		editTextPasswordRepeat = (EditText) findViewById(R.id.create_account_password_repeat_field);
		buttonCreateAccount = (Button) findViewById(R.id.create_account_button);
		
		buttonCreateAccount.setOnClickListener(new OnCreateAccount());
	}
	
	private class OnCreateAccount implements OnClickListener {

		@Override
		public void onClick(View v) {
			Boolean create = true;
			if(editTextLogin.getText().toString().equals("")) {
				editTextLogin.setError(getText(R.string.field_cannot_be_null));
				create = false;
			}
			if(editTextPassword.getText().toString().equals("")) {
				editTextPassword.setError(getText(R.string.field_cannot_be_null));
				create = false;
			}
			if(!editTextPassword.getText().toString().equals(editTextPasswordRepeat.getText().toString())) {
				editTextPassword.setError(getText(R.string.passwords_must_be_the_same));
				create = false;
			}
			if(create) {
				new CreateAccountTask().execute(new String[] {editTextLogin.getText().toString(), editTextPassword.getText().toString()});
			}
			
		}
		
	}
	
	private class CreateAccountTask extends AsyncTask<String, Void, Result> {
		ProgressDialog dialog;
		@Override
		protected void onPreExecute() {
			dialog = ProgressDialog.show(CreateAccountActivity.this, getText(R.string.app_name), getText(R.string.aura_create_account), false, true, new OnCancelProgressDialog());
		}

		@Override
		protected Result doInBackground(String... params) {
			Result result = Aura.getInstance().getUserAPI().createAccount(params[0], params[1]);
			return result;
		}

		@Override
		protected void onPostExecute(Result result) {
			dialog.dismiss();
			if(result.getResult() == true) {
				returnToLoginActivity(result);
			} else {
				if(result.getDescription().equals("lib_nickname_already_exists")) {
					editTextLogin.setError(getText(R.string.nickname_already_exists));
				} else if(result.getDescription().equals("lib_server_error")) {
					Toast.makeText(CreateAccountActivity.this, getText(R.string.server_error), Toast.LENGTH_LONG).show();
				}
			}
			
		}
		
		private class OnCancelProgressDialog implements OnCancelListener {

			@Override
			public void onCancel(DialogInterface dialog) {
				CreateAccountTask.this.cancel(true);
			}
			
		}
	}

	public void returnToLoginActivity(Result result) {
		Intent in = new Intent();
		in.putExtra(NICK_NAME, editTextLogin.getText().toString());
		in.putExtra(PASSWORD, editTextPassword.getText().toString());
		in.putExtra(USER_ID, result.getValueInteger(AuraProtocolMapKeys.USER_ID_KEY));
		setResult(Activity.RESULT_OK, in);
		finish();
	}
}
